package com.shitu.soulemo.a;

import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shitu.base.net.model.Tabs;
import com.shitu.soulemo.R;
import com.shitu.soulemo.rn.RNPayActivity;
import com.shitu.soulemo.utils.DateFormat;

import java.util.ArrayList;
import java.util.List;

public class RequireAdapter extends RecyclerView.Adapter<RequireAdapter.RequireViewHolder> {
    private List<Tabs> data = new ArrayList<>();
    private Fragment fr;

    public RequireAdapter(Fragment fragment) {
        fr = fragment;
    }

    public void setData(List<Tabs> data) {
        this.data.clear();
        this.data = data;
    }

    public void setData(List<Tabs> data1, List<Tabs> data2) {
        data.clear();
        Tabs tab1 = new Tabs();
        tab1.setName("附近资源推荐");
        this.data.add(tab1);
        this.data.addAll(data1);
        Tabs tab2 = new Tabs();
        tab2.setName("推荐");
        this.data.add(tab2);
        this.data.addAll(data2);
    }

    @Override
    public RequireViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RequireViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.r_list_item, null));
    }

    @Override
    public void onBindViewHolder(RequireViewHolder holder, int position) {
        final Tabs tab = data.get(position);
        if (tab.getHopeWorkPlace() == null) {
            //labelView
            holder.main.setVisibility(View.GONE);
            holder.empty.setVisibility(View.VISIBLE);
            holder.empty.setText(tab.getName());
        } else {
            holder.main.setVisibility(View.VISIBLE);
            holder.empty.setVisibility(View.GONE);
            holder.name.setText(tab.getName());
            holder.address.setText(tab.getHopeWorkPlace());
            if (tab.getIsBill()) {
                holder.check.setVisibility(View.VISIBLE);
            } else {
                holder.check.setVisibility(View.INVISIBLE);
            }
//            holder.item.setText(CategoryMap.getString(tab.getCategoryId()));
            holder.time.setText(DateFormat.parse(tab.getCreateTime()) + " 至 " + DateFormat.parse(tab.getEndTime()));
            holder.info.setText(tab.getPrice() + "元" + "， 数量：" + tab.getCount() + "人，" + "时间：" + tab.getTimeCount() + "天");
            if (tab.getProviderUser() != null) {
                Glide.with(holder.avatar).load(tab.getProviderUser().getAvatar()).into(holder.avatar);
                holder.user.setText(tab.getProviderUser().getTrueName());
            }
            holder.main.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (fr.getActivity() != null) {
                        RNPayActivity.start(fr.getActivity(), tab.getId(), tab);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class RequireViewHolder extends RecyclerView.ViewHolder {
        public TextView name;
        public TextView item;
        public TextView check;
        public TextView info;
        public TextView time;
        public TextView distance;
        public TextView address;
        public ImageView avatar;
        public TextView button;
        public TextView user;
        public TextView title;
        public TextView empty;
        public LinearLayout main;

        public RequireViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            item = itemView.findViewById(R.id.item);
            check = itemView.findViewById(R.id.check);
            info = itemView.findViewById(R.id.info);
            time = itemView.findViewById(R.id.time);
            distance = itemView.findViewById(R.id.distance);
            address = itemView.findViewById(R.id.address);
            avatar = itemView.findViewById(R.id.avatar);
            button = itemView.findViewById(R.id.button);
            user = itemView.findViewById(R.id.user);
            title = itemView.findViewById(R.id.title);
            empty = itemView.findViewById(R.id.empty);
            main = itemView.findViewById(R.id.main);
        }
    }
}
